package com.qingluan.filecachetest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileCache {
	private static final String FileDir  = "ImageCache";
	private static final String WHOLESALE_CONV = ".cach";
	private Context context;
	private File FilesDir;
	private static final int BUFFER = 8192;
	
	
	public static void saveFile(File file,byte[] data ) throws IOException {
		DataOutputStream DOS = null;
		try {
			
			DOS = new DataOutputStream(new FileOutputStream(file));
			DOS.write(data);
		}finally {
			if (DOS != null){
				DOS.close();
			}
		}
	}
	
	public static byte[] readFile(File file) throws IOException{
		DataInputStream DIS = null;
		byte[] byte_data = null;
		if (!file.exists()){
			return null;
		}
		try{
			DIS = new DataInputStream(new FileInputStream(file));
			byte_data = new  byte[DIS.available()];
			DIS.read(byte_data);
			
		}finally{
			if (DIS != null){
				DIS.close();
			}
		}
		return byte_data;
		
	}
	
	public static void CopyFile(File file_from , File file_to) throws IOException {
		BufferedInputStream BIS = null;
		BufferedOutputStream BOS = null;
		try{
			BIS =new BufferedInputStream(new FileInputStream(file_from));
			BOS = new BufferedOutputStream(new FileOutputStream(file_to));
			byte[] buffer = new byte[BUFFER];
			int length ;
			while ((length = BIS.read(buffer)) != -1){
				BOS.write(buffer,0,length);
			}
		}finally {
			if (BOS != null){
				BOS.close();
			}
			if (BIS != null){
				BIS.close();
			}
		}
	}

	
	
	
	
	
}
